package com.lbd.email.service.impl;

import com.lbd.base.service.BaseServiceImpl;
import com.lbd.email.bean.EmailLog;
import com.lbd.email.conttoller.vo.EmailLogVo;
import com.lbd.email.dao.EmailLogRepository;
import com.lbd.email.service.EmailLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * Created by wang on 2016/9/23.
 */
@Service
@Transactional
public class EmailLogServiceImpl extends BaseServiceImpl<EmailLog, EmailLogRepository> implements EmailLogService {

    @Autowired
    EmailLogRepository emailLogRepository;


    /**
     * 构建邮件模板信息的查询条件和参数
     *
     * @param emailLog  条件
     * @param parameter 参数
     * @return
     */
    @Override
    public String buildJpql(EmailLog emailLog, Map<String, Object> parameter) {
        StringBuilder jpqlBuilder = new StringBuilder("  from EmailLog t  ");
        EmailLogVo emailLogVo = (EmailLogVo) emailLog;
        if (emailLogVo != null) {
            if (null != emailLogVo.getConsignee()) {
                jpqlBuilder.append(" and  t.consignee like :consignee");
                parameter.put("consignee", "%" + emailLogVo.getConsignee() + "%");
            }
            if (null != emailLogVo.getStartDate()) {
                jpqlBuilder.append(" and  createdDate >= :startDate ");
                parameter.put("startDate", emailLogVo.getStartDate());
            }
            if (null != emailLogVo.getEndDate()) {
                jpqlBuilder.append(" and  createdDate <= :endDate");
                parameter.put("endDate", emailLogVo.getEndDate());
            }
        }
        jpqlBuilder.append(" order by createdDate desc");
        return jpqlBuilder.toString();
    }


}
